﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.Xml.Serialization
Imports System.Xml
Namespace CMISObjects
   <XmlRoot("permission", Namespace:=CmisNamespaceConstants.Cmis)>
   Public Class AccessControlList
      Implements IXmlSerializable

      Public Property PrincipalIds As String()
      Public Property Permissions As String()
      Public Property Direct As Boolean

      Public Sub New()

      End Sub

      Public Sub New(principalIds() As String, permissions As String())
         Me.PrincipalIds = principalIds
         Me.Permissions = permissions
      End Sub

      Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
         Return Nothing
      End Function

      Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
         Dim doc As New XmlDocument()
         doc.Load(reader)
         Dim principalIds As New List(Of String)
         Dim permissions As New List(Of String)
         Dim direct As Boolean = False

         Dim node As XmlNode = Common.GetItemCaseInsensitiveFromXML(doc, "permission")
         For Each child As XmlNode In node.ChildNodes
            Dim lName As String = Common.GetNodeNameWithoutPrefix(child).ToLowerInvariant
            If lName.Equals("principal") Then
               For Each principalChild As XmlNode In child.ChildNodes
                  If Common.GetNodeNameWithoutPrefix(principalChild).ToLowerInvariant.Equals("principalid") Then
                     principalIds.Add(principalChild.InnerText)
                  End If
               Next
            ElseIf lName.Equals("permission") Then
               permissions.Add(child.InnerText)
            ElseIf lName.Equals("direct") Then
               direct = Boolean.Parse(child.InnerText)
            End If
         Next

         Me.PrincipalIds = principalIds.ToArray
         Me.Permissions = permissions.ToArray
         Me.Direct = direct

      End Sub


      Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
         writer.WriteStartElement("principal", CmisNamespaceConstants.Cmis)
         If PrincipalIds IsNot Nothing Then
            For Each id As String In PrincipalIds
               writer.WriteElementString("principalId", CmisNamespaceConstants.Cmis, id)
            Next
         End If
         writer.WriteEndElement() 'Startelement: principal
         If Permissions IsNot Nothing Then
            For Each Permission As String In Permissions
               writer.WriteElementString("permission", CmisNamespaceConstants.Cmis, Permission)
            Next
         End If
         writer.WriteElementString("direct", CmisNamespaceConstants.Cmis, Direct.ToString.ToLowerInvariant)
      End Sub
   End Class
End Namespace